package com.hddf.project.auth.actions;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.kommii.log.LogNames;

import com.hddf.project.auth.json_util.JsonProvider;
import com.hddf.project.auth.pojos.Authority;
import com.hddf.project.auth.pojos.User;
import com.hddf.project.auth.service.AuthService;
import com.hddf.project.auth.service.MongoDB_Query;
import com.hddf.project.auth.service.MongoDB_Sync;
import com.mongodb.DBObject;

public class AuthAction extends BaseAction{
	
	private static final long serialVersionUID = 159997542775L;
	private int id;
	private String name;
	private int pid; //暂时不使用
	private String url;
	private int isleaf; //暂时不是使用
	private int sort; 
	private int userid;
	private String username;
	private Date update_time;
	private int state;
	
	@SuppressWarnings("finally")
	public String add(){
		HttpServletRequest request = getRequest();
		Map<String,Object> map = new HashMap<String,Object>();
		String state = "";
		String msg = "";
		try {
			AuthService as = AuthService.getInstance();
			MongoDB_Sync ms = MongoDB_Sync.getInstance();
			HashMap m = (HashMap)request.getAttribute("user");
			System.out.println("auth add bigen3");
			User u = (User)m.get("u");
			username = u.getUsername();
			userid = u.getId();
			Authority auth = new Authority();
			Date date = new Date();
			auth.setIsleaf(1);
			auth.setName(name);
			auth.setSort(sort);
			auth.setUpdate_time(date);
			auth.setUrl(url);
			auth.setState(1);
			auth.setUserid(userid);
			auth.setUsername(username);
			boolean b = as.addAuth(auth);
			boolean b2 = false;
			if(b){
				b2 = ms.addAuth(auth.toDbObject());
			}
			if(b && b2){
				state = "ok";
				request.setAttribute("logname",LogNames.auth_manager);
				request.setAttribute("logs","add auth id: "+auth.getId()+" ,add auth name: "+name);
			}
		} catch (Exception e) {
			e.printStackTrace();
			state = "error";
			msg = "添加权限异常";
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			request.setAttribute("result", JsonProvider.getCommonJson(map));
			return "simpleResult";
		}
	}
	
	@SuppressWarnings("finally")
	public String delete(){
		HttpServletRequest request = getRequest();
		Map<String,Object> map = new HashMap<String,Object>();
		String state = "";
		String msg = "";
		try {
			AuthService as = AuthService.getInstance();
			MongoDB_Sync ms = MongoDB_Sync.getInstance();
			HashMap m = (HashMap)request.getAttribute("user");
			//System.out.println("result：  "+result);
			User u = (User)m.get("u");
			username = u.getUsername();
			userid = u.getId();
			if(id==0){
				state = "error";
				msg = "删除权限失败";
			}else{
				boolean b = as.deleteAuth(id);
				boolean b2 = ms.deleteAuth(id);
				if(b && b2){
					state = "ok";
					request.setAttribute("logname",LogNames.auth_manager);
					request.setAttribute("logs","delete id: "+id);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			state = "error";
			msg = "删除权限异常";
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			request.setAttribute("result", JsonProvider.getCommonJson(map));
			return "simpleResult";
		}
	}
	
	@SuppressWarnings("finally")
	public String update(){
		HttpServletRequest request = getRequest();
		Map<String,Object> map = new HashMap<String,Object>();
		String status = "";
		String msg = "";
		try {
			AuthService as = AuthService.getInstance();
			MongoDB_Sync ms = MongoDB_Sync.getInstance();
			HashMap m = (HashMap)request.getAttribute("user");
			User u = (User)m.get("u");
			username = u.getUsername();
			userid = u.getId();
			Authority auth = new Authority();
			Date date = new Date();
			auth.setId(id);
			auth.setIsleaf(1);
			auth.setName(name);
			auth.setSort(sort);
			auth.setUpdate_time(date);
			auth.setUrl(url);
			auth.setState(state);
			auth.setUserid(userid);
			auth.setUsername(username);
			boolean rs = as.updateAuth(auth);
			if(rs){
				DBObject obj = auth.toDbObject();
				rs = ms.updateAuth(obj);
				if(rs){
					status = "ok";
					request.setAttribute("logname",LogNames.auth_manager);
					request.setAttribute("logs","update auth id: "+auth.getId()+" ,update auth name: "+name);
				}
			}else{
				status = "error";
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			status = "error";
			msg = "修改权限异常";
		} finally {
			map.put("state", status);
			map.put("msg", msg);
			request.setAttribute("result", JsonProvider.getCommonJson(map));
			return "simpleResult";
		}
	}
	
	@SuppressWarnings("finally")
	public String getAuthList(){
		HttpServletRequest request = getRequest();
		Map<String,Object> map = new HashMap<String,Object>();
		String state = "";
		String msg = "";
		try {
			//AuthService as = AuthService.getInstance();
			MongoDB_Query mq = MongoDB_Query.getInstance();
			//List<Authority> list = as.getAuthList(null, null, -1);
			List<DBObject> list = mq.getAuthList(null,null,-1);
			//int totalCount = as.getAuthTotalCount(-1);
			int totalCount = mq.getAuthTotalCount(-1);
			if(null != list){
				state = "ok";
				map.put("auth_list", list);
				map.put("totalCount", totalCount);
				request.setAttribute("logname",LogNames.auth_select);
				request.setAttribute("logs","select auth list ");
			}else{
				state = "error";
			}
		} catch (Exception e) {
			e.printStackTrace();
			state = "error";
			msg = "获取权限列表异常";
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			request.setAttribute("result", JsonProvider.getCommonJson(map));
			return "simpleResult";
		}
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public int getPid() {
		return pid;
	}


	public void setPid(int pid) {
		this.pid = pid;
	}


	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public int getIsleaf() {
		return isleaf;
	}


	public void setIsleaf(int isleaf) {
		this.isleaf = isleaf;
	}


	public int getSort() {
		return sort;
	}


	public void setSort(int sort) {
		this.sort = sort;
	}


	public int getUserid() {
		return userid;
	}


	public void setUserid(int userid) {
		this.userid = userid;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public Date getUpdate_time() {
		return update_time;
	}


	public void setUpdate_time(Date update_time) {
		this.update_time = update_time;
	}


	public int getState() {
		return state;
	}


	public void setState(int state) {
		this.state = state;
	}

}
